﻿package org.utils {
    import flash.display.Graphics;
    import flash.display.*;
    import flash.geom.Point;
    public class DrawShape {

		/**
		 *封装绘制图形类，支持绘制虚线，直线，圆，长方形，圆角长方形 ,三角形
		 * 
		 */		       
        public function DrawShape():void { }
		/**
		 * 虚线
		 * @param linethick 虚线粗线
		 * @param linecolor 虚线颜色
		 * @param p1 虚线端点坐标
		 * @param p2 虚线端点坐标
		 * @param leng 虚线长度
		 * @param gap 虚线间隔
		 * @return Shape 
		 */		
		public static  function dotted(linethick:Number,linecolor:uint,p1:Point,p2:Point,leng:Number=5,gap:Number=5):Sprite   
        {   
			var child:Sprite = new Sprite();
			var graphics:Graphics=child.graphics
            var max:Number = Point.distance(p1,p2);    
            var l:Number = 0;    
            var p3:Point;    
            var p4:Point; 
			graphics.lineStyle(linethick, linecolor); 
            while(l<max)    
           {    
            p3 = Point.interpolate(p2,p1,l/max);    
            l+=leng;    
            if(l>max)l=max    
            p4 = Point.interpolate(p2,p1,l/max);    
            graphics.moveTo(p3.x,p3.y)    
            graphics.lineTo(p4.x,p4.y)    
            l+=gap;    
           }
			return child
        }   
		/**
		 * 直线
		 * @param linethick 线条粗细
		 * @param linecolor 线条颜色
		 * @param p1 线条端点坐标
		 * @param p2 线条端点坐标
		 * @return Sprite 
		 * 
		 */        
		public static function line(linethick:Number,linecolor:uint,p1:Point,p2:Point):Sprite {

			  var child:Sprite = new Sprite();
              child.graphics.lineStyle(linethick, linecolor);
              child.graphics.moveTo(p1.x, p1.y)
			  child.graphics.lineTo(p2.x, p2.y)
			  child.graphics.endFill();
			  return child
 
		}
		
		/**
		 * 三角形
		 * @param linethick 线条粗细
		 * @param linecolor 线条颜色
		 * @param p1 线条端点坐标
		 * @param p2 线条端点坐标
		 * @param p3 线条端点坐标
		 * @return Sprite 
		 * 
		 */        
		public static function triangle(linethick:Number,linecolor:uint,p1:Point,p2:Point,p3:Point):Sprite {
			
			var child:Sprite = new Sprite();
			
			child.graphics.lineStyle(linethick, linecolor);
			child.graphics.beginFill(0xACA899,1)
			child.graphics.moveTo(p1.x, p1.y)
			child.graphics.lineTo(p2.x, p2.y)
			child.graphics.lineTo(p3.x, p3.y)
			child.graphics.lineTo(p1.x, p1.y)
			child.graphics.endFill()
				
			return child
			
		}
		
		/**
		 * 菱形
		 * @param linethick 线条粗细
		 * @param linecolor 线条颜色
		 * @param p1 线条端点坐标
		 * @param p2 线条端点坐标
		 * @param p3 线条端点坐标
		 * @param p4 线条端点坐标
		 * @return Sprite 
		 * 
		 */        
		public static function diamond(linethick:Number,linecolor:uint,p1:Point,p2:Point,p3:Point,p4:Point,fillalpha:Number=1,fillcolor:uint=0x000000):Sprite {
			
			var child:Sprite = new Sprite();
			child.graphics.lineStyle(linethick, linecolor);
			child.graphics.beginFill(fillcolor,fillalpha)
			child.graphics.moveTo(p1.x, p1.y)
			child.graphics.lineTo(p2.x, p2.y)
			child.graphics.lineTo(p3.x, p3.y)
			child.graphics.lineTo(p4.x, p4.y)
			child.graphics.lineTo(p1.x, p1.y)
			child.graphics.endFill()
			return child
			
		}
		
		/**
		 * 圆 
		 * @param linethick 边框粗细
		 * @param linecolor 边框颜色
		 * @param fillcolor 填充颜色
		 * @param fillalpha 填充透明度
		 * @param p 圆中心坐标
		 * @param r 圆半径
		 * @return  Sprite 
		 * 
		 */		
		public static function circle(linethick:Number,linecolor:uint,fillcolor:Number,fillalpha:Number,p:Point,r:Number):Sprite {
            var child:Sprite = new Sprite();
            child.graphics.beginFill(fillcolor,fillalpha);
            child.graphics.lineStyle(linethick, linecolor);
            child.graphics.drawCircle(p.x, p.y, r);
            child.graphics.endFill();
            return child;
        }
		/**
		 * 圆角长方形 
		 * @param linethick 边框粗细
		 * @param linecolor 边框颜色
		 * @param fillcolor 填充颜色
		 * @param fillalpha 填充透明度
		 * @param p1 绘制开始鼠标按下坐标
		 * @param p2 绘制结束鼠标弹起坐标
		 * @param cornerRadius 圆角弧度参数
		 * @return Sprite
		 * 
		 */        
		public static function roundRect(linethick:Number,linecolor:uint,cornerRadius:Number,p1:Point,p2:Point,fillcolor:uint=0xffffff,fillalpha:Number=1):Sprite {
            var child:Sprite = new Sprite();
            child.graphics.beginFill(fillcolor,fillalpha);
            child.graphics.lineStyle(linethick, linecolor);
            child.graphics.drawRoundRect(p1.x, p1.y, p2.x-p1.x, p2.y-p2.y, cornerRadius);
            child.graphics.endFill();
			return child;
        }
		
		/**
		 * 长方形 
		 * @param linethick 边框粗细
		 * @param linecolor 边框颜色
		 * @param fillcolor 填充颜色
		 * @param fillalpha 填充透明度
		 * @param p1 绘制开始鼠标按下坐标
		 * @param p2 绘制结束鼠标弹起坐标
		 * @return Sprite
		 * 
		 */		
		public static function rect(linethick:Number,linecolor:uint,p1:Point,p2:Point,fillcolor:uint=0xffffff,fillalpha:Number=1):Sprite {
			var child:Sprite = new Sprite();
			child.graphics.beginFill(fillcolor,fillalpha);
			child.graphics.lineStyle(linethick, linecolor);
			child.graphics.drawRect(p1.x, p1.y, p2.x-p1.x, p2.y-p2.y);
			child.graphics.endFill();
			return child;
		}
		

    }
}
